package com.atguigu.xuexiplus.controller;

import com.atguigu.xuexiplus.dto.HomeworkDTO;
import com.atguigu.xuexiplus.dto.HomeworkVO;
import com.atguigu.xuexiplus.result.Result;
import com.atguigu.xuexiplus.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业控制器
 */
@RestController
@RequestMapping("/api/homework")
@CrossOrigin(origins = "*")
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;
    /**
     * 发布作业
     */
    @PostMapping
    public Result<String> publishHomework(@RequestBody HomeworkDTO homeworkDTO) {
        boolean result = homeworkService.publishHomework(homeworkDTO);
        return result ? Result.success("作业发布成功") : Result.error("作业发布失败");
    }

    /**
     * 根据学科ID查询作业列表
     */
    @GetMapping("/subject/{subjectId}")
    public Result<List<HomeworkVO>> getHomeworksBySubjectId(@PathVariable Long subjectId) {
        List<HomeworkVO> homeworks = homeworkService.getHomeworksBySubjectId(subjectId);
        return Result.success(homeworks);
    }

    /**
     * 根据学科ID和学生ID查询作业列表（包含完成状态）
     */
    @GetMapping("/subject/{subjectId}/student/{studentId}")
    public Result<List<HomeworkVO>> getHomeworksBySubjectIdAndStudent(
            @PathVariable Long subjectId, 
            @PathVariable Long studentId) {
        List<HomeworkVO> homeworks = homeworkService.getHomeworksBySubjectIdAndStudent(subjectId, studentId);
        return Result.success(homeworks);
    }

    /**
     * 查询今日作业列表
     */
    @GetMapping("/today")
    public Result<List<HomeworkVO>> getTodayHomeworks() {
        List<HomeworkVO> homeworks = homeworkService.getTodayHomeworks();
        return Result.success(homeworks);
    }

    /**
     * 查询今日作业（按学生，含完成状态）
     */
    @GetMapping("/today/student/{studentId}")
    public Result<List<HomeworkVO>> getTodayHomeworksByStudent(@PathVariable Long studentId) {
        List<HomeworkVO> homeworks = homeworkService.getTodayHomeworksByStudent(studentId);
        return Result.success(homeworks);
    }

    /**
     * 切换完成状态（完成<->未完成）
     */
    @PostMapping("/{homeworkId}/toggle/{studentId}")
    public Result<String> toggleCompletion(@PathVariable Long homeworkId, @PathVariable Long studentId) {
        boolean ok = homeworkService.toggleCompletion(homeworkId, studentId);
        return ok ? Result.success("切换成功") : Result.error("切换失败");
    }

    /**
     * 查询今日作业完成度
     */
    @GetMapping("/today/completion/{studentId}")
    public Result<Double> getTodayCompletionRate(@PathVariable Long studentId) {
        Double completionRate = homeworkService.getTodayCompletionRate(studentId);
        return Result.success(completionRate);
    }

    /**
     * 更新作业
     */
    @PutMapping("/{id}")
    public Result<String> updateHomework(@PathVariable Long id, @RequestBody HomeworkDTO homeworkDTO) {
        boolean result = homeworkService.updateHomework(id, homeworkDTO);
        return result ? Result.success("作业更新成功") : Result.error("作业更新失败");
    }

    /**
     * 删除作业
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteHomework(@PathVariable Long id) {
        boolean result = homeworkService.deleteHomework(id);
        return result ? Result.success("作业删除成功") : Result.error("作业删除失败");
    }

    /**
     * 测试接口
     */
    @GetMapping("/test")
    public Result<String> test() {
        return Result.success("作业系统API正常");
    }

    /**
     * 查询所有作业（调试用）
     */
    @GetMapping("/all")
    public Result<List<HomeworkVO>> getAllHomeworks() {
        List<HomeworkVO> homeworks = homeworkService.getTodayHomeworks();
        System.out.println("查询到作业数量: " + homeworks.size());
        return Result.success(homeworks);
    }
}




